package service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.Customer;
import entity.LayRequest;
import entity.Linkman;
import entity.Role;
import mapper.CustomerMapper;
import mapper.LinkmanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class CustomerServiceImpl implements CustomerService{
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private LinkmanMapper linkmanMapper;
    @Override
    public int insert(Customer customer) {

        customerMapper.insert(customer);
        customer.getLinkman().setId(customer.getId());
        int i = linkmanMapper.insert(customer.getLinkman());
        return i;

    }

    @Override
    public List<Customer> selectAll() {
        return customerMapper.selectAll();
    }


    @Override
    public Customer selectById(Integer id) {
        return customerMapper.selectById(id);
    }

    @Override
    public boolean delete(Integer id) {
        return customerMapper.delete(id)>0?true:false;
    }

    @Override
    public boolean update(Customer customer) {
        return customerMapper.update(customer)>0?true:false;
    }

    @Override
    public PageInfo<Customer> selectByPage(Map<String, Object> map) {
        int pageNum=map.get("pageNum")==null?1
                :Integer.valueOf(map.get("pageNum").toString());
        int pageSize=map.get("pageSize")==null?1
                :Integer.valueOf(map.get("pageSize").toString());
        //1.设置当前页和每页记录条数
        PageHelper.startPage(pageNum,pageSize);
        //2.执行查询，startPage之后的第一个查询默认会进行分页
//        List<Admin> admins = adminMapper.selectByMap(map);
        List<Customer> admins = customerMapper.selectWithRole(map);
        //3. 创建PageInfo对象，并将返回结果传入。会自动赋值总页数，总记录数当前页等数据
        PageInfo<Customer> adminPageInfo = new PageInfo<>(admins);
        return adminPageInfo;
    }

    @Override
    public PageInfo<Customer> selectByPage(LayRequest layRequest) {
        //1.设置当前页和每页记录数
        PageHelper.startPage(layRequest.getPage(),layRequest.getLimit());
        //2.执行查询
        List<Customer> customers=customerMapper.selectByMap(layRequest.getParams());
        //3.创建PageInfo对象
        return new PageInfo<>(customers);
    }

    @Override
    public boolean updateHeadPic(Customer customer) {
        return customerMapper.updateHeadPic(customer)>0?true:false;
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return customerMapper.batchDelete(ids);
    }

}
